package com.fireline.beauty.frame;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TypedListener;
import org.eclipse.wb.swt.SWTResourceManager;

/**
 * 功能导航面版
 * */
public class FunctionComposite extends Composite {
	// 选中时面版背景色
	Color color = SWTResourceManager.getColor(212, 231, 186);

	public final String id;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	FunctionComposite(Composite parent, Image image, String title,
			String describe, String id) {
		super(parent, SWT.NONE);
		this.id = id;
		setBackgroundMode(SWT.INHERIT_FORCE);
		setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		GridLayout gridLayout = new GridLayout(2, false);
		gridLayout.horizontalSpacing = 20;
		setLayout(gridLayout);

		CLabel labImage = new CLabel(this, SWT.NONE);
		labImage.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false,
				1, 2));
		labImage.setImage(image);
		CLabel labTitle = new CLabel(this, SWT.NONE);
		labTitle.setFont(SWTResourceManager.getFont("宋体", 12, SWT.BOLD));
		labTitle.setText("New Label");
		labTitle.setText(title);
		CLabel labDescribe = new CLabel(this, SWT.NONE);
		labDescribe.setForeground(SWTResourceManager
				.getColor(SWT.COLOR_DARK_GRAY));
		labDescribe.setText("New Label");
		labDescribe.setText(describe);

		// 初始化监听事件
		addListener(SWT.MouseExit, funLisener);
		addListener(SWT.MouseEnter, funLisener);
		addListener(SWT.MouseUp, funLisener);
		addListener(SWT.MouseDoubleClick, funLisener);
		for (Control c : getChildren()) {
			c.addListener(SWT.MouseExit, funLisener);
			c.addListener(SWT.MouseEnter, funLisener);
			c.addListener(SWT.MouseUp, funLisener);
			c.addListener(SWT.MouseDoubleClick, funLisener);
		}

	}

	public void addSelectionListener(SelectionListener listener) {
		checkWidget();
		if (listener == null)
			SWT.error(SWT.ERROR_NULL_ARGUMENT);
		TypedListener typedListener = new TypedListener(listener);
		addListener(SWT.Selection, typedListener);
		addListener(SWT.DefaultSelection, typedListener);
		for (Control c : getChildren()) {
			c.addListener(SWT.Selection, typedListener);
			c.addListener(SWT.DefaultSelection, typedListener);
		}
	}

	final Listener funLisener = new Listener() {
		@Override
		public void handleEvent(Event event) {
			if (event.type == SWT.MouseExit) {
				setBackground(null);
			} else if (event.type == SWT.MouseEnter) {
				setBackground(color);
			} else if (event.type == SWT.MouseUp) {
				event.widget=FunctionComposite.this;
				event.type = SWT.Selection;
			} else if (event.type == SWT.MouseDoubleClick) {
				event.widget=FunctionComposite.this;
				event.type = SWT.DefaultSelection;
			}
		}
	};

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
